/* Aleksey Matyushev
 *
 * This program is the sole property of Aleksey
 * Matyushev. This program is designed to be a GUI
 * add-on to DATCOM with extended capabilities in
 * providing XML data for FlightGear.
 */

package datcom2;

import java.io.FileNotFoundException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JPanel;

public class for005 extends javax.swing.JPanel {

    public for005(String Path, String workingPath, String caseID, TERMINATION terminationPanel, MAIN mainClass) {
        initComponents();
        this.path = Path;
        this.workingPath = workingPath;
        this.caseID = caseID;
        storeText();
        this.terminationPanel = terminationPanel;
        this.mainClass = mainClass;
    }

    public for005(String Path, String workingPath, String caseID, MAIN mainClass, JPanel mainPanel) {
        initComponents();
        this.path = Path;
        this.workingPath = workingPath;
        this.caseID = caseID;
        this.mainClass = mainClass;
        this.mainPanel = mainPanel;
        storeText();
    }

    public void storeText(){
        try {
            jTextArea1.setText(printFile.getText(path));
        } catch (FileNotFoundException ex) {
            Logger.getLogger(for005.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();

        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(datcom2.DATCOM2App.class).getContext().getResourceMap(for005.class);
        jLabel2.setFont(resourceMap.getFont("jLabel2.font")); // NOI18N
        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jTextArea1.setName("jTextArea1"); // NOI18N
        jScrollPane1.setViewportView(jTextArea1);

        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText(resourceMap.getString("jButton3.text")); // NOI18N
        jButton3.setName("jButton3"); // NOI18N
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jTextField1.setText(resourceMap.getString("jTextField1.text")); // NOI18N
        jTextField1.setName("jTextField1"); // NOI18N

        jTextField2.setText(resourceMap.getString("jTextField2.text")); // NOI18N
        jTextField2.setName("jTextField2"); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addGap(65, 65, 65)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel1)
                                    .addComponent(jLabel3))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 332, Short.MAX_VALUE)
                                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                        .addComponent(jTextField2, javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jTextField1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 379, Short.MAX_VALUE))))
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 713, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jButton1))
                .addGap(32, 32, 32))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 334, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 16, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton3)
                    .addComponent(jButton2))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // If user selects to run DATCOM
        for005 = jTextArea1.getText();
        
        String[] openDatcom = {workingPath + "\\Datcom.bat"};
        String mkDir = "cmd /c mkdir " + workingPath + "\\" + caseID;
        
        runBabyRun.executeDatcom(mkDir);
        
        printFile.writeFile(workingPath + "\\" + caseID, "for005.dat", for005);

        batchFile.writeFile(workingPath, caseID);

        runBabyRun.executeDatcom(openDatcom);

        for005panel =  this;
        reviewPanel = new REVIEW(workingPath + "\\" + caseID + "\\for006.dat", for005panel, mainClass);
        reviewPanel.showFor006();
        
        //switch panels
        setVisible(false);
        mainClass.setComponent(reviewPanel);
        reviewPanel.setVisible(true);
    }//GEN-LAST:event_jButton1ActionPerformed

    private String[] getArrayFromString (String tempField){
        String tempArrayString = "";
        int arrayCounter = 0;
        int countARRAYnumbersINfield = 0;

        char[] tempFieldArray = tempField.toCharArray();

        for(int z = 0; z < tempFieldArray.length;z++)
        {
            if(tempFieldArray[z] == ','){
                countARRAYnumbersINfield ++;
            }
        }

        //System.out.println("found this many numbers: " + countARRAYnumbersINfield);

        String[] ARRAY = new String[countARRAYnumbersINfield];

        for (int k = 0; k < tempFieldArray.length; k++){
            if (tempFieldArray[k] == ','){
               ARRAY[arrayCounter] = tempArrayString;
               //System.out.println("This is the ARRAY number i am storing: " + tempArrayString);
               tempArrayString = "";
               arrayCounter++;
            } else {
                   tempArrayString = tempArrayString + tempFieldArray[k];
            }
        }

        return ARRAY;
    }

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // if user selects to run C3PO

        //grab the for005 file from the text area
        for005 = jTextArea1.getText();

        //grab the MACH numbers from the text field
        String tempMACH = jTextField2.getText();
        MACH = getArrayFromString(tempMACH);
        
        //grab the ALT numbers from the text field
        String tempALT = jTextField1.getText();
        ALT = getArrayFromString(tempALT);

        //create a new instance of C3PO
        runMore = new C3PO(for005);

        //create a MASTER XML list
        String mkDir = "cmd /c mkdir " + workingPath + "\\" + caseID + "\\C3PO\\MASTER";
        runBabyRun.executeDatcom(mkDir);
        
        //Run DATCOM for each corresponding ALTITUDE and MACH number
        for (int i = 0; i < ALT.length; i++)
        {
            int position = 0;
            for(int j = 0; j < MACH.length; j++){

            String[] openC3PO = {workingPath + "\\Datcom.bat"};

            String mkDirMACH = "cmd /c mkdir " + workingPath + "\\" + caseID + "\\C3PO\\" +ALT[i] +"\\"+ MACH[j]+"_" + ALT[i];
            for005 = runMore.alterFile(MACH[j],ALT[i]);

            //alter the command string
            batchFile.writeFile( workingPath, workingPath + "\\" + caseID + "\\C3PO\\" +ALT[i] +"\\"+ MACH[j]+"_" + ALT[i], caseID);
            runBabyRun.executeDatcom(mkDirMACH);
            
            //write the for005 File into the folder
            printFile.writeFile(workingPath + "\\" + caseID + "\\C3PO\\"+ALT[i] +"\\"+ MACH[j]+"_" + ALT[i], "for005.dat", for005);

            //run DATCOM
            runBabyRun.executeDatcom(openC3PO);

            //grab the aero parameters
            runMore.grabParameters(workingPath + "\\" + caseID + "\\C3PO\\" +ALT[i] +"\\"+ MACH[j]+"_" + ALT[i]);
            runMore.createMaster(workingPath + "\\" + caseID + "\\C3PO\\" +ALT[i] +"\\"+ MACH[j]+"_" + ALT[i], MACH[j], ALT[i], position,workingPath + "\\" + caseID + "\\C3PO\\MASTER\\" );
            position++;
            }
            runMore.writeMaster();
            position = 0;
        }

        for005panel = this;
        reviewPanel = new REVIEW(workingPath + "\\" + caseID + "\\C3PO\\" + ALT[ALT.length-1] + "\\" + MACH[MACH.length-1] +
                "_" + ALT[ALT.length-1] + "\\for006.dat", for005panel, mainClass,"Aero Tables: " +
                workingPath + "\\" + caseID +
                "\\C3PO\\MASTER\\");
        reviewPanel.showFor006();

        //switch panels
        setVisible(false);
        mainClass.setComponent(reviewPanel);
        reviewPanel.setVisible(true);
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // if the user presses the BACK button, the pervious frame is shown
        setVisible(false);
        if (terminationPanel != null){
            terminationPanel.setVisible(true);
            mainClass.setComponent(terminationPanel);
        } else {
            mainPanel.setVisible(true);
            mainClass.setComponent(mainPanel);
        }
    }//GEN-LAST:event_jButton3ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    // End of variables declaration//GEN-END:variables

    private String path;
    private String workingPath;
    private String caseID;
    private String for005;

    private getFor005 printFile = new getFor005();
    private runDatcom runBabyRun = new runDatcom();
    private writeBAT batchFile = new writeBAT();

    private C3PO runMore;
    private TERMINATION terminationPanel;
    private REVIEW reviewPanel;
    private MAIN mainClass;
    private for005 for005panel;
    private JPanel mainPanel;

    //temp C3PO inputs
    private String[] ALT;
    private String[] MACH;
    //END temp C3PO inputs
}
